package yuan.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import yuan.feign_client.ProviderService;
import yuan.handler.BlockExceptionHandler;
import yuan.handler.FallBackExceptionHandler;

import javax.annotation.Resource;

/**
 * @version: 1.0
 * @Description: TODO
 * @author: zZmH
 * @date: 2020/09/20 19:56
 */


@RestController
@RequestMapping("/consumer")
public class Controller {

    @GetMapping("/get")
    public String get(){
        return "dsf";
    }

    @GetMapping("/testHotKey")
    @SentinelResource(value = "testHotKey",blockHandler = "testHotKeyExceptionHandler")
    public String testHotKey(@RequestParam("p")String sb){

        return sb;
    }

    public String testHotKeyExceptionHandler(String sb, BlockException e) {

        return "error!";
    }


    @GetMapping("/testwo")
    @SentinelResource(value = "testwo",
            blockHandlerClass = BlockExceptionHandler.class, blockHandler = "blockhandler",
            fallbackClass = FallBackExceptionHandler.class, fallback = "fallBack",
            exceptionsToIgnore = IllegalStateException.class)
    public String testwo(@RequestParam("p")String sb){


        if(sb.equals("sb"))
            throw new NullPointerException();

        if ("bs".equals(sb)) {
            throw new IllegalStateException();
        }
        return sb;

    }

    @Resource
    ProviderService providerService;


    @GetMapping("/provider/get")
    public String sdf(){
        return providerService.get();
    }

}
